package com.example.store2.Controller;

import com.example.store2.Service.UserService;
import com.example.store2.Utils.JwtUtils;
import com.example.store2.pojo.Result;
import com.example.store2.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        log.info("用户登录:{}",user);
        User user1=userService.login(user);
        if(user1 !=null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", user1.getId());
            claims.put("name", user1.getName());
            claims.put("username", user1.getUsername());
            String jwt = JwtUtils.generateJwt(claims); //jwt包含了当前登录的员工信息
            return Result.success(jwt);
        }
        return Result.error("用户名或者密码错误");
    }
}
